//app.js
App({
    onLaunch: function () {
        console.log('App Launch');
        //调用API从本地缓存中获取数据
        var logs = wx.getStorageSync('logs') || [];
        logs.unshift(Date.now());
        wx.setStorageSync('logs', logs);
    },

    onShow: function () {
        console.log('App Show');
    },

    onHide: function () {
        console.log('App Hide');
    },

    globalData: {
        userInfo: null,
        hasLogin: false,
        openid: null,
        session_key: null,
        appId: null,
        loginInfo: null
    },

    // lazy loading openid
    getUserInfo: function (cb) {
        console.log('App getUserInfo');

        var that = this;
        if (that.globalData.userInfo) {
            typeof cb == "function" && cb(that.globalData.userInfo)
            wx.getUserInfo({
                withCredentials: false,
                success: function (res) {
                    that.globalData.userInfo = res.userInfo
                    typeof cb == "function" && cb(that.globalData.userInfo)
                }
            })
        } else {
            //调用登录接口
            wx.login({
                success: function (res) {
                    console.log('login_success', res);

                    wx.request({
                        url: 'https://api.weixin.qq.com/sns/jscode2session?appid=wxefce01e1ff5e8393&secret=e3af28448b5fa87b5e6c1cfb71df3c4f&js_code=' + res.code+'&grant_type=authorization_code',
                        success: function(res){
                            console.log('login_request_success', res);
                            that.globalData.session_key = res.data.session_key;
                            that.globalData.appId = 'wxefce01e1ff5e8393';
                        }
                    })

                    wx.getUserInfo({
                        withCredentials: true,
                        success: function (res) {
                            that.globalData.userInfo = res.userInfo
                            typeof cb == "function" && cb(that.globalData.userInfo)
                        }
                    })
                }
            })
        }
    },

    getUserOpenId: function (callback) {
        console.log('App getUserOpenId');

        var self = this;

        if (self.globalData.openid) {
            callback(null, self.globalData.openid)
        } else {
            wx.login({
                success: function (data) {
                    wx.request({
                        url: 'https://todayhot.bj.1253539676.clb.myqcloud.com/openid',
                        data: {
                            code: data.code
                        },
                        success: function (res) {
                            console.log('拉取openid成功', res);
                            self.globalData.openid = res.data.openid
                            callback(null, self.globalData.openid)
                        },
                        fail: function (res) {
                            console.log('拉取用户openid失败，将无法正常使用开放接口等服务', res)
                            callback(res)
                        }
                    })
                },
                fail: function (err) {
                    console.log('wx.login 接口调用失败，将无法正常使用开放接口等服务', err)
                    callback(err)
                }
            })
        }
    },

    login: function () {
        var that = this
        // wx.login({
        //     success: function (res) {
        //         console.log("login-success", res);



        //         that.globalData.hasLogin = true;

        //         that.getUserInfo((userInfo) => {
        //             console.log("userInfo", userInfo);
        //         });

        //         that.getUserOpenId(() => {

        //         });
        //     }
        // });

        that.getUserInfo((userInfo) => {
            console.log("userInfo", userInfo);
        });
    }
})